﻿using System.Windows;
using System.Windows.Controls;

namespace ControlLibrary.Controls;

public class MessageHost : ItemsControl
{
    // 注册一个名为MessageQueue的依赖属性
    public static readonly DependencyProperty QueueProperty =
        DependencyProperty.Register(nameof(MessageQueue), typeof(MessageHostQueue), typeof(MessageHost),
            new PropertyMetadata(null, OnQueueChanged));

    // 静态构造函数，设置默认样式
    static MessageHost()
    {
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof(MessageHost),
            new FrameworkPropertyMetadata(typeof(MessageHost)));
    }

    // 获取或设置MessageQueue属性
    public MessageHostQueue MessageQueue
    {
        get => (MessageHostQueue)GetValue(QueueProperty);
        set => SetValue(QueueProperty, value);
    }

    // 当Queue属性发生变化时，更新ItemsSource属性
    private static void OnQueueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is not MessageHost host) return;
        host.ItemsSource = e.NewValue switch
        {
            MessageHostQueue newQueue => newQueue.Items,
            _ => null
        };
    }
}